package com.meihua.shellapp.adapter.personalcenter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.meihua.shellapp.R;
import com.meihua.shellapp.activity.webview.WebActivity;
import com.meihua.shellapp.base.MyApplication;
import com.meihua.shellapp.databean.ShopDataBean;
import com.meihua.shellapp.utils.DateUtils;
import com.meihua.shellapp.utils.PicassoUtils;
import com.meihua.shellapp.utils.URL;
import com.meihua.shellapp.view.FullRoundEdgeImageView;

import java.util.List;

/**
 * 作者：lc on 2017/9/27.
 * 邮箱：lc0607live@163.com
 */

public class MyFollowAdapter extends RecyclerView.Adapter<MyFollowAdapter.BrowseHandler> {

    private List<ShopDataBean> mData;
    private Context mContext;

    public MyFollowAdapter(Context context, List<ShopDataBean> data) {
        mData = data;
        mContext = context;
    }


    @Override
    public BrowseHandler onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_myfollow, null);
        BrowseHandler handler = new BrowseHandler(view);
        return handler;
    }

    @Override
    public void onBindViewHolder(BrowseHandler holder, final int position) {
        PicassoUtils.getInstance().bindImageData(holder.iv_photo, mData.get(position).getProduct_img());
        holder.tv_details.setText(mData.get(position).getDetails());
        holder.tv_money.setText("￥" + 88);
        holder.tv_money.setVisibility(View.GONE);//隐藏掉金额
        holder.tv_name.setText(mData.get(position).getProduct_name());
        if (isFirstTime(DateUtils.getInstance().getTime(mData.get(position).getCreate_time()), position)) {
            holder.ll_top.setVisibility(View.VISIBLE);
            holder.tv_time.setText(DateUtils.getInstance().getTime(mData.get(position).getCreate_time()));
        } else {
            holder.ll_top.setVisibility(View.GONE);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent in = new Intent(mContext, WebActivity.class);
                in.putExtra("url", URL.getUrl().WEBPRODUCT + mData.get(position).getId() + "/" + MyApplication.getInstance().getMId());
                mContext.startActivity(in);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    public void setMData(List<ShopDataBean> data) {
        this.mData = data;
        notifyDataSetChanged();
    }

    public class BrowseHandler extends RecyclerView.ViewHolder {

        TextView tv_time, tv_name, tv_details, tv_money;
        FullRoundEdgeImageView iv_photo;
        LinearLayout ll_top;

        public BrowseHandler(View itemView) {
            super(itemView);
            tv_time = itemView.findViewById(R.id.tv_time);
            tv_name = itemView.findViewById(R.id.tv_name);
            tv_details = itemView.findViewById(R.id.tv_details);
            tv_money = itemView.findViewById(R.id.tv_money);
            iv_photo = itemView.findViewById(R.id.iv_photo);
            ll_top = itemView.findViewById(R.id.ll_top);
        }
    }

    private boolean isFirstTime(String time, int position) {
        for (int i = 0; i < mData.size(); i++) {
            if (DateUtils.getInstance().getTime(mData.get(i).getCreate_time()).equals(time)) {
                if (i == position) {
                    return true;
                } else {
                    return false;
                }
            }
        }
        return false;
    }
}
